//
//  Grade.m
//  类似于点评_评分
//
//  Created by dpfst520 on 16/2/19.
//  Copyright © 2016年 NttData.dang. All rights reserved.
//

#import "Grade.h"

@interface Grade ()
{
    float starRating;
    float lastRating;
    
    float height;
    float width;
    
    UIImage *unSelectedImage;
    UIImage *halfSelectedImage;
    UIImage *fullSelectedImage;
    
}

@property (nonatomic, strong)UIImageView *star1;
@property (nonatomic, strong)UIImageView *star2;
@property (nonatomic, strong)UIImageView *star3;
@property (nonatomic, strong)UIImageView *star4;
@property (nonatomic, strong)UIImageView *star5;
@property (nonatomic, strong)UIImageView *star6;

@property (nonnull, assign)id<GradeDelegate> delegate;



@end

@implementation Grade

/**
 *  初始化设置未选中图片、半选中图片、全选中图片，以及评分值改变的代理（可以用
 *  Block）实现
 *
 *  @param deselectedName   未选中图片名称
 *  @param halfSelectedName 半选中图片名称
 *  @param fullSelectedName 全选中图片名称
 *  @param delegate          代理
 */
- (void)setImageDeselected:(NSString *)deselectedName halfSelected:(NSString *)halfSelectedName fullSelected:(NSString *)fullSelectedName andDelegate:(id<GradeDelegate>)delegate
{
    self.delegate = delegate;
    
    unSelectedImage = [UIImage imageNamed:deselectedName];
    
    halfSelectedImage = halfSelectedName == nil ? unSelectedImage : [UIImage imageNamed:halfSelectedName];
    
    fullSelectedImage = [UIImage imageNamed:fullSelectedName];
    
    height = 0.0,width = 0.0;
    
    if (height < [fullSelectedImage size].height) {
        height = [fullSelectedImage size].height;
    }
    if (height < [halfSelectedImage size].height) {
        height = [halfSelectedImage size].height;
    }
    if (height < [unSelectedImage size].height) {
        height = [unSelectedImage size].height;
    }
    if (width < [fullSelectedImage size].width) {
        width = [fullSelectedImage size].width;
    }
    if (width < [halfSelectedImage size].width) {
        width = [halfSelectedImage size].width;
    }
    if (width < [unSelectedImage size].width) {
        width = [unSelectedImage size].width;
    }
    
    //控件宽度适配
    CGRect frame = [self frame];
    
    CGFloat viewWidth = width * 5;
    if (frame.size.width > viewWidth) {
        viewWidth = frame.size.width;
    }
    frame.size.width = viewWidth;
    frame.size.height = height;
    [self setFrame:frame];
    
    starRating = 0.0;
    lastRating = 0.0;
    
    _star1 = [[UIImageView alloc] initWithImage:unSelectedImage];
    _star2 = [[UIImageView alloc] initWithImage:unSelectedImage];
    _star3 = [[UIImageView alloc] initWithImage:unSelectedImage];
    _star4 = [[UIImageView alloc] initWithImage:unSelectedImage];
    _star5 = [[UIImageView alloc] initWithImage:unSelectedImage];
    
    
    //星星图片之间的间距
    CGFloat space = (CGFloat)(viewWidth - width*5)/6;
    
    CGFloat startX = space;
    [_star1 setFrame:CGRectMake(startX, 0, width, height)];
    startX += width + space;
    [_star2 setFrame:CGRectMake(startX, 0, width, height)];
    startX += width + space;
    [_star3 setFrame:CGRectMake(startX, 0, width, height)];
    startX += width + space;
    [_star4 setFrame:CGRectMake(startX, 0, width, height)];
    startX += width + space;
    [_star5 setFrame:CGRectMake(startX, 0, width, height)];
    
    [_star1 setUserInteractionEnabled:NO];
    [_star2 setUserInteractionEnabled:NO];
    [_star3 setUserInteractionEnabled:NO];
    [_star4 setUserInteractionEnabled:NO];
    [_star5 setUserInteractionEnabled:NO];
    
    [self addSubview:_star1];
    [self addSubview:_star2];
    [self addSubview:_star3];
    [self addSubview:_star4];
    [self addSubview:_star5];

}

/**
 *  设置评分值
 *
 *  @param rating 评分值
 */
- (void)displayRating:(float)rating
{
    [_star1 setImage:unSelectedImage];
    [_star2 setImage:unSelectedImage];
    [_star3 setImage:unSelectedImage];
    [_star4 setImage:unSelectedImage];
    [_star5 setImage:unSelectedImage];
    
    if (rating >= 0.5) {
        [_star1 setImage:halfSelectedImage];
    }
    if (rating >= 1) {
        [_star1 setImage:fullSelectedImage];
    }
    if (rating >= 1.5) {
        [_star2 setImage:halfSelectedImage];
    }
    if (rating >= 2) {
        [_star2 setImage:fullSelectedImage];
    }
    if (rating >= 2.5) {
        [_star3 setImage:halfSelectedImage];
    }
    if (rating >= 3) {
        [_star3 setImage:fullSelectedImage];
    }
    if (rating >= 3.5) {
        [_star4 setImage:halfSelectedImage];
    }
    if (rating >= 4) {
        [_star4 setImage:fullSelectedImage];
    }
    if (rating >= 4.5) {
        [_star5 setImage:halfSelectedImage];
    }
    if (rating >= 5) {
        [_star5 setImage:fullSelectedImage];
    }
    
    starRating = rating;
    lastRating = rating;
    [_delegate ratingBar:self ratingChanged:rating];
}

/**
 *  获取当前的评分值
 *
 *  @return 评分值
 */
- (float)rating
{
    return starRating;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self touchesRating:touches];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self touchesRating:touches];
}

//触发事件
- (void)touchesRating:(NSSet *)touches
{
    if (self.isIndicator) {
        
        return;
    }
    
    CGPoint point = [[touches anyObject] locationInView:self];
    
    //星星图片之间的间距
    CGFloat space = (CGFloat)(self.frame.size.width - width * 5) / 6;
    
    float newRating = 0;
    
    if (point.x >= 0 && point.x <= self.frame.size.width) {
        
        if (point.x <= space + width * 0.5f) {
            
            newRating = 0.5;
            
        }else if (point.x < space * 2 + width){
            
            newRating = 1.0;
            
        }else if (point.x < space * 2 + width * 1.5){
            
            newRating = 1.5;
            
        }else if (point.x <= 3 * space + 2 * width){
            
            newRating = 2.0;
            
        }else if (point.x <= 3 * space + 2.5 * width){
            
            newRating = 2.5;
            
        }else if (point.x <= 4 * space + 3 * width){
            
            newRating = 3.0;
            
        }else if (point.x <= 4 * space + 3.5 * width){
            
            newRating = 3.5;
            
        }else if (point.x <= 5 * space + 4 * width){
            
            newRating = 4.0;
            
        }else if (point.x <= 5 * space + 4.5 * width){
            
            newRating = 4.5;
            
        }else {
            
            newRating = 5.0;
        }
        
    }
    
    if (newRating != lastRating){
        [self displayRating:newRating];
    }

    


}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
